const store = {
  dispatch: () => {
    console.log('我是原始的dispatch');
  },
}

let dispatch = store.dispatch;

const middlewareAPI = {
  dispatch: action => dispatch(action),
  // dispatch: store.dispatch,
};

function compose(...funcs) {
  if (funcs.length === 0) {
    return arg => arg;
  }
  if (funcs.length === 1) {
    return funcs[0];
  }
  return funcs.reduce((a, b) => (...args) => a(b(...args)));
}

const a = (store) => {
  return (next) => () => {
    dispatch = function() {
      console.log('我是改过的dispatch');
    }
    console.log('a pre');
    next();
    console.log('a after');
  };
}
const b = store => {
  store.dispatch();
  return (next) => () => { console.log('b pre'); next(); console.log('b after') }
}

const c = (store) => {
  store.dispatch();
  return (next) => () => { console.log('c pre'); next(); console.log('c after') }
}

let chain = [a, b, c].map(middleware =>
  middleware(middlewareAPI));

dispatch = compose(...chain)(store.dispatch);

dispatch();
